package com.sicau.socialpracticesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sicau.socialpracticesystem.common.myenum.PracticeNewsStateEnum;
import com.sicau.socialpracticesystem.common.myenum.TeamProgressEnum;
import com.sicau.socialpracticesystem.config.dozer.DozerUtils;
import com.sicau.socialpracticesystem.entity.param.practicenews.PracticeNewsAddParam;
import com.sicau.socialpracticesystem.entity.param.practicenews.PracticeNewsQueryParam;
import com.sicau.socialpracticesystem.entity.param.practicenews.PracticeNewsUpdateParam;
import com.sicau.socialpracticesystem.entity.param.practicenews.PracticeNewsUpdateStateParam;
import com.sicau.socialpracticesystem.entity.po.PracticeNews;
import com.sicau.socialpracticesystem.entity.po.Team;
import com.sicau.socialpracticesystem.entity.po.TeamMember;
import com.sicau.socialpracticesystem.entity.vo.practicenews.PracticeNewsVO;
import com.sicau.socialpracticesystem.mapper.PracticeNewsMapper;
import com.sicau.socialpracticesystem.mapper.VOMapper;
import com.sicau.socialpracticesystem.service.PracticeNewsService;
import com.sicau.socialpracticesystem.service.TeamMemberService;
import com.sicau.socialpracticesystem.service.TeamService;
import com.sicau.socialpracticesystem.util.FileUtil;
import com.sicau.socialpracticesystem.util.UserContext;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 团队新闻稿表 服务实现类
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-04-26
 */
@Service
public class PracticeNewsServiceImpl extends ServiceImpl<PracticeNewsMapper, PracticeNews> implements PracticeNewsService {

    @Resource
    private DozerUtils dozerUtils;
    @Resource
    PracticeNewsMapper practiceNewsMapper;
    @Resource
    TeamMemberService teamMemberService;
    @Resource
    VOMapper.PracticeNewsVOMapper practiceNewsVOMapper;
    @Resource
    TeamService teamService;

    @Override
    public void add(PracticeNewsAddParam practiceNewsAddParam) {

        //判断是否是这团队的
        TeamMember teamMember = teamMemberService.queryByTeamIdAndUserId(practiceNewsAddParam.getTeamId(), UserContext.get().getId());
        if (teamMember == null){
            throw new RuntimeException("当前用户不是该团队成员");
        }

        Team team = teamService.getById(practiceNewsAddParam.getTeamId());
        TeamProgressEnum teamProgressEnum = TeamProgressEnum.getEnum(team.getProgress());
        if (teamProgressEnum == TeamProgressEnum.INITIATE ||
            teamProgressEnum == TeamProgressEnum.STARTED ||
            teamProgressEnum == TeamProgressEnum.NEWS){
            throw new RuntimeException("所属团队当前阶段无法提交新闻稿");
        }


        PracticeNews practiceNews = dozerUtils.map(practiceNewsAddParam, PracticeNews.class);
        practiceNews.setCover(practiceNewsAddParam.getCover());
        practiceNews.setCreatorId(UserContext.get().getId());
        practiceNews.setCreatedTime(LocalDateTime.now());
        practiceNews.setState(PracticeNewsStateEnum.AUDITING.getState());
        practiceNewsMapper.insert(practiceNews);
    }

    @Override
    public String uploadFile(MultipartFile file) {
        return FileUtil.writeFileToLocal(file);
    }

    @Override
    public void update(PracticeNewsUpdateParam param) {
        PracticeNews practiceNews = practiceNewsMapper.selectById(param.getId());

        //判断是否是这团队的
        TeamMember teamMember = teamMemberService.queryByTeamIdAndUserId(practiceNews.getTeamId(), UserContext.get().getId());
        if (teamMember == null){
            throw new RuntimeException("当前用户不是该团队成员");
        }

        practiceNews.setContent(param.getContent());
        practiceNews.setTitle(param.getTitle());
        practiceNews.setState(PracticeNewsStateEnum.AUDITING.getState());

        practiceNewsMapper.updateById(practiceNews);

    }

    @Override
    public IPage<PracticeNewsVO> queryPage(PracticeNewsQueryParam param) {
        Page<PracticeNewsVO> page = new Page<>(param.getCurrentPage(), param.getPageSize());
        LambdaQueryWrapper<PracticeNewsVO> queryWrapper = new LambdaQueryWrapper<PracticeNewsVO>()
                .eq(!ObjectUtils.isEmpty(param.getTeamId()), PracticeNewsVO::getTeamId, param.getTeamId())
                .eq(!ObjectUtils.isEmpty(param.getState()), PracticeNewsVO::getState, param.getState())
                .like(!ObjectUtils.isEmpty(param.getTitle()), PracticeNewsVO::getTitle, param.getTitle())
                .like(!ObjectUtils.isEmpty(param.getCreatorName()), PracticeNewsVO::getCreatorName, param.getCreatorName())
                .orderByDesc(PracticeNewsVO::getId);
        return practiceNewsVOMapper.selectPage(page, queryWrapper);
    }

    @Override
    public void updateState(PracticeNewsUpdateStateParam param) {
        if (null == PracticeNewsStateEnum.getEnum(param.getState())) {
            throw new RuntimeException("传入类型有误");
        }
        PracticeNews practiceNews = dozerUtils.map(param, PracticeNews.class);
        practiceNewsMapper.updateById(practiceNews);

        //如果通过了，则更新团队状态
        if (param.getState().equals(PracticeNewsStateEnum.PASS.getState())){
            PracticeNews byId = practiceNewsMapper.selectById(param.getId());
            Team team = new Team();
            team.setId(byId.getId());
            team.setProgress(TeamProgressEnum.NEWS.getId());
            teamService.updateById(team);
        }
    }

    @Override
    public PracticeNewsVO queryVOById(Integer id) {
        return practiceNewsVOMapper.selectById(id);
    }
}
